<?php

namespace App\Http\Controllers\API;

use App\Http\Requests\API\CreateFreightAPIRequest;
use App\Http\Requests\API\UpdateFreightAPIRequest;
use App\Models\Freight;
use App\Repositories\FreightRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;
use InfyOm\Generator\Criteria\LimitOffsetCriteria;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

/**
 * Class FreightController
 * @package App\Http\Controllers\API
 */

class FreightAPIController extends AppBaseController
{
    /** @var  FreightRepository */
    private $freightRepository;

    public function __construct(FreightRepository $freightRepo)
    {
        $this->freightRepository = $freightRepo;
    }

    /**
     * Display a listing of the Freight.
     * GET|HEAD /freights
     *
     * @param Request $request
     * @return Response
     */
    public function index(Request $request)
    {
        $this->freightRepository->pushCriteria(new RequestCriteria($request));
        $this->freightRepository->pushCriteria(new LimitOffsetCriteria($request));
        $freights = $this->freightRepository->all();

        return $this->sendResponse($freights->toArray(), 'Freights retrieved successfully');
    }

    /**
     * Store a newly created Freight in storage.
     * POST /freights
     *
     * @param CreateFreightAPIRequest $request
     *
     * @return Response
     */
    public function store(CreateFreightAPIRequest $request)
    {
        $input = $request->all();

        $freights = $this->freightRepository->create($input);

        return $this->sendResponse($freights->toArray(), 'Freight saved successfully');
    }

    /**
     * Display the specified Freight.
     * GET|HEAD /freights/{id}
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        /** @var Freight $freight */
        $freight = $this->freightRepository->findWithoutFail($id);

        if (empty($freight)) {
            return $this->sendError('Freight not found');
        }

        return $this->sendResponse($freight->toArray(), 'Freight retrieved successfully');
    }

    /**
     * Update the specified Freight in storage.
     * PUT/PATCH /freights/{id}
     *
     * @param  int $id
     * @param UpdateFreightAPIRequest $request
     *
     * @return Response
     */
    public function update($id, UpdateFreightAPIRequest $request)
    {
        $input = $request->all();

        /** @var Freight $freight */
        $freight = $this->freightRepository->findWithoutFail($id);

        if (empty($freight)) {
            return $this->sendError('Freight not found');
        }

        $freight = $this->freightRepository->update($input, $id);

        return $this->sendResponse($freight->toArray(), 'Freight updated successfully');
    }

    /**
     * Remove the specified Freight from storage.
     * DELETE /freights/{id}
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        /** @var Freight $freight */
        $freight = $this->freightRepository->findWithoutFail($id);

        if (empty($freight)) {
            return $this->sendError('Freight not found');
        }

        $freight->delete();

        return $this->sendResponse($id, 'Freight deleted successfully');
    }
}
